package org.wak.scene
{
	import org.wak.baseoneonline.flash.geom.IntPoint;

	/**
	 * Creates a SceneDefinition from an XML containing the data.
	 * 
	 */	
	public class XMLSceneDefinition implements ISceneDefinition
	{
		
		// Id of the Scene 
		private var mSceneId:String = "";
		
		// Background of the Scene 
		private var mBackgroundId:String = "";
		
		// Size of the tile inside the grid. 
		private var mTileSize:int = 0 ;
		
		// Array with a list of blocked tiles for walk in IntPoint s.   
		private var mBlockedForWalkTiles:Array = null;
		
		// CharacterPlayer id on the Scene.
		private var mCharacterId:String = "";
		
		// Player starting point
		private var mPlayerStartingPoint:IntPoint = null;
		
		// Player walk speed
		private var mPlayerWalkSpeed:Number = 50;
		
		// Debug Scene enabled.
		private var mDebugScene:Boolean = false;
		
		// Scale decremente rate, for each pixel away from the camera the scale will be decreased
		// 1X this value.
		private var mScaleRate:Number = 0;
		
		// This value is added to the scaling formula to give a greater max value of scaling.
		private var mScaleBase:Number = 0;
		
		// the array containing the items in the Scene.
		private var mSceneItemsArray:Array ;
		
		// walk behinds area definitions.
		private var mWalkBehindsArray:Array ;
		
		// id of the ambiental sound of the Scene.
		private var mAmbientalSndId:String = "";
		
		// Array of gatespots in the scene
		private var mGateSpotsArray:Array = null ;
		
		// Array of actionSpots in the scene
		private var mActionSpotsArray:Array = null ;
		
		// Array of SceneCharData
		private var mCharsOnSceneArray:Array = null ;
		
		
		
		public function XMLSceneDefinition(pXMLDef:XML)
		{
			mSceneId = pXMLDef.@id ;
			mBackgroundId = pXMLDef.Background ;
			mTileSize = pXMLDef.TileSize ;
			
			
			mBlockedForWalkTiles = new Array();
			for each( var intpt:XML in pXMLDef.BlockedTiles.IntPoint )
			{
				mBlockedForWalkTiles.push( new IntPoint(intpt.@x, intpt.@y) );
			}
			
			mCharacterId = pXMLDef.PlayerCharacter ; 
			
			// TODO: extract int point
			var fAuxPt:XML = pXMLDef.StartingPoint.IntPoint[0] ; 
			mPlayerStartingPoint = new IntPoint( fAuxPt.@x, fAuxPt.@y );
			
			
			
			mPlayerWalkSpeed = pXMLDef.WalkSpeed ;
			mScaleRate = pXMLDef.ScaleRate ;
			mScaleBase = pXMLDef.ScaleBase ;
			
			mDebugScene = pXMLDef.DebugScene == "true"? true : false ;
			
			
			mSceneItemsArray = new Array();
			
			// extract the scene items
			var fLItemDef:ISceneItemDefinition = null;
			var fNewItem:SceneItem = null;
			for each( var item:XML in pXMLDef.SceneItems.SceneItem )
			{
				fLItemDef = new XMLItemDefinition( item );
				fNewItem = new SceneItem( fLItemDef );
				mSceneItemsArray[ fNewItem.interactableId ] = fNewItem;
			}
			
			// extract the walk area items
			var fNewWalkBehindDef:XMLWalkBehindDef = null ;
			mWalkBehindsArray = new Array() ;
			for each( var area:XML in pXMLDef.WalkBehinds.WalkBehindArea )
			{
				fNewWalkBehindDef = new XMLWalkBehindDef(area);
				mWalkBehindsArray.push(fNewWalkBehindDef);
			}
			mAmbientalSndId = pXMLDef.AmbientalAudio ;
			
			
			// extract gatespots
			var fNewGSDef:XMLGateSpotDefinition = null ;
			mGateSpotsArray = new Array();
			for each( var gspot:XML in pXMLDef.Hotspots.GateSpot )
			{
				fNewGSDef = new XMLGateSpotDefinition( gspot );
				mGateSpotsArray.push( new GateSpot( fNewGSDef ) );
			}
			
			// extract actionspots
			var fNewASDef:XMLActionSpotDefinition = null ;
			mActionSpotsArray = new Array(); 
			for each( var asspot:XML in pXMLDef.Hotspots.ActionSpot)
			{
				fNewASDef = new XMLActionSpotDefinition(asspot);
				mActionSpotsArray.push( new ActionSpot( fNewASDef ) );
			}
			
			// extract the characters on scene.
			mCharsOnSceneArray = new Array();
			var fNewCharData:SceneCharData = null;
			for each( var charData:XML in pXMLDef.SceneCharacters.Character )
			{
				fNewCharData = new SceneCharData(charData);
				mCharsOnSceneArray.push( fNewCharData );
			}
			
			/* 
			trace(mBlockedForWalkTiles) ;
			trace("mPlayerStartingPoint:"+mPlayerStartingPoint);
			trace("mSceneId:"+mSceneId);
			trace("mBackgroundId:"+mBackgroundId); 
			trace("mTileSize:"+mTileSize); 
			trace("mCharacterId:"+mCharacterId);
			trace("mPlayerWalkSpeed:"+mPlayerWalkSpeed);
			trace("mScaleRate:"+mScaleRate); 
			trace("mScaleBase:"+mScaleBase);   
			 */
			
			
		}

		public function get debugEnabled():Boolean
		{
			return mDebugScene;
		}
		
		public function get playerWalkSpeed():Number
		{
			return mPlayerWalkSpeed;
		}
		
		public function get playerStartingPoint():IntPoint
		{
			return mPlayerStartingPoint;
		}
		
		public function get playerCharacter():String
		{
			return mCharacterId ;
		}
		
		public function get blockedForWalkArray():Array
		{
			return mBlockedForWalkTiles;
		}
		
		public function get backgroundId():String
		{
			return mBackgroundId;
		}
		
		public function get sceneId():String
		{
			return mSceneId;
		}
		
		public function get tileSize():int
		{
			return mTileSize;
		}
		
		public function get scaleRate():Number
		{
			return mScaleRate;
		}
		
		public function get scaleBase():Number
		{
			return mScaleBase;
		}
		
		public function get sceneItems():Array
		{
			return mSceneItemsArray ;
		}
		
		public function get walkBehinds():Array 
		{
			return mWalkBehindsArray ;
		}
		
		public function get ambientalSoundId():String
		{
			return mAmbientalSndId ;
		}
		
		public function get gatespots():Array
		{
			return mGateSpotsArray ;
		}
		
		public function get actionspots():Array
		{
			return mActionSpotsArray ;
		}
		
		public function get charactersOnScene():Array
		{
			return mCharsOnSceneArray ;
		}
		
		
		
	}
}